package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import model.Model;
import model.TokboxDAO;

import databean.Tokbox;
import databean.User;


public class VideoViewConfAction extends Action {
	
	
	private TokboxDAO tokboxDAO;


	public VideoViewConfAction(Model model) {
		tokboxDAO = model.getTokboxDAO();
	}

	public String getName() { return "video-view-conf.do"; }

	public String perform(HttpServletRequest request,HttpServletResponse response) {
        
		// Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        Tokbox tokbox;
        
        HttpSession session = request.getSession();
        
		try {
	        
			if (session.getAttribute("user") == null) {
				return "user-login.do";
			}
	        
			request.setAttribute("user",(User) request.getSession(false).getAttribute("user"));
			
			
			String confName = request.getParameter("confName");
	    	String type = request.getParameter("type");
			
	    	
	    	if (type.equals("join")) {
	    		tokbox = new Tokbox();
	    		try {
	    			tokbox = tokboxDAO.getTokboxByConfName(confName);

	    			request.setAttribute("tokbox", tokbox);
	    			
	    		} catch (Exception e) {
	    			errors.add(e.getMessage());
	            	return "video-view-conf.jsp";
	    		}
	    		
	    	} else {
	    		errors.add("no type specified");
	        	return "video-view-conf.jsp";
	    	}
			

	        return "video-view-conf.jsp";
	        
        } catch (Exception e) {
        	errors.add(e.getMessage());
        	return "video-view-conf.jsp";
        }
        
    }
	
}



